package com.sdjz.club.club.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdjz.club.club.entity.Event;
import com.sdjz.club.club.entity.UserEvent;
import com.sdjz.club.club.mapper.EventMapper;
import com.sdjz.club.club.mapper.UserEventMapper;
import com.sdjz.club.club.service.impl.EventServiceImp;
import com.sdjz.club.club.utils.ImageUtil;
import com.sdjz.club.club.utils.SnowFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Dftre
 * @since 2022-07-21
 */
@RestController
@RequestMapping("/club/event")
public class EventController {
    @Autowired
    EventServiceImp eventService;
    @Autowired
    EventMapper eventMapper;
    @Autowired
    EventServiceImp eventServiceImp;
    @Autowired
    UserEventMapper userEventMapper;

    //查询数据总条数
    @RequestMapping("total")
    public CommonResult selectLength() {
        return new CommonResult(eventService.count());
    }

    //查询
    @GetMapping("/select")
    public CommonResult select(
            @RequestParam(name = "id", required = false, defaultValue = "0") Long id,
            @RequestParam(name = "proprieter_id", required = false, defaultValue = "0") Long proprieter_id,
            @RequestParam(name = "organization_id", required = false, defaultValue = "0") Long organization_id,
            @RequestParam(name = "type", required = false, defaultValue = "") List<String> type,
            @RequestParam(name = "introduce", required = false, defaultValue = "null") String introduce,
            @RequestParam(name = "people_number", required = false, defaultValue = "0") Integer people_number,
            @RequestParam(name = "sign_in", required = false, defaultValue = "false") Boolean sign_in,
            @RequestParam(name = "name", required = false, defaultValue = "null") String name,
            @RequestParam(name = "event_name", required = false, defaultValue = "null") String event_name,
            @RequestParam(name = "phonenumber", required = false, defaultValue = "null") String phonenumber,
            @RequestParam(name = "address", required = false, defaultValue = "") List<String> address,
            @RequestParam(name = "department", required = false, defaultValue = "") List<String> department,
            @RequestParam(name = "statu", required = false, defaultValue = "1") Integer statu
    ) {
        QueryWrapper<Event> wrapper = new QueryWrapper<>();
        if (statu.equals(1)) wrapper.eq("statu", statu);
        if (!id.equals(0L)) wrapper.eq("id", id);
        if (!proprieter_id.equals(0L)) wrapper.eq("proprieter_id", proprieter_id);
        if (!organization_id.equals(0L)) wrapper.eq("organization_id", organization_id);
        if (type.size() != 0) wrapper.in("type", type);
        if (!introduce.equals("null")) wrapper.like("introduce", introduce);
//        if (!img_id.equals("null")) wrapper.like("img_id", img_id);
        if (!people_number.equals(0)) wrapper.eq("people_number", people_number);
        if (sign_in.equals(Boolean.TRUE)) wrapper.eq("sign_in", sign_in);
//        if (!start_time.equals("null")) wrapper.like("start_time", start_time);
//        if (!end_time.equals("null")) wrapper.like("end_time", end_time);
        if (!name.equals("null")) wrapper.like("name", name);

        if (!phonenumber.equals("null")) wrapper.eq("phonenumber", phonenumber);
        if (address.size() != 0) wrapper.in("address", address);
        if (department.size() != 0) wrapper.in("department", department);
        if (!event_name.equals("null")) wrapper.like("event_name", event_name);
        return new CommonResult(eventMapper.selectList(wrapper));
    }

    // 实现分页查询
    @GetMapping("list")
    public CommonResult selectPage(
            @RequestParam(name = "current") int current,
            @RequestParam(name = "size", required = false, defaultValue = "20") int size,
            @RequestParam(name = "isPending", required = false, defaultValue = "pending") String pending
    ) {
        QueryWrapper<Event> wrapper = new QueryWrapper<>();
        if (pending.equals("pending")) wrapper.eq("statu", 0);
        else wrapper.eq("statu", 1);
        Page<Event> page = new Page<>(current, size);
        List<Map> mapList = new ArrayList<>();
        eventMapper.selectPage(page, wrapper);
        return new CommonResult(page.getRecords());
    }

    // 实现分页查询
    @GetMapping("listV2")
    public CommonResult selectPageV2(
            @RequestParam(name = "current") int current,
            @RequestParam(name = "size", required = false, defaultValue = "20") int size
    ) {
        Page<Event> page = new Page<>(current, size);
        List<Map> mapList = new ArrayList<>();
        eventMapper.selectPage(page, null);
        page.getRecords().forEach(i -> {
            Map map = JSON.parseObject(JSON.toJSONString(i), new TypeReference<Map<String, Object>>() {
            });
            map.put("imgId", "https://www.codingyt.com/getImage/" + map.get("imgId"));
            mapList.add(map);
        });
        return new CommonResult(mapList);
    }

    // 实现删除
    @PostMapping("/delete")
    public CommonResult deleteMenu(@RequestParam("id") String id) {
        return CommonResult.succ(eventServiceImp.delete(id));
    }

    // 实现插入
    @PostMapping("/insert")
    public CommonResult addMenu(
            @RequestParam("userId") String userId,
            @RequestParam("eventName") String event_name,
            @RequestParam("address") String address,
            @RequestParam("type") String type,
            @RequestParam("department") String department,
            @RequestParam("startTime") String start_time,
            @RequestParam("endTime") String end_time,
            @RequestParam("name") String name,
            @RequestParam("signIn") Boolean sign_in,
            @RequestParam("introduce") String introduce,
            @RequestParam("img") MultipartFile img,
            @RequestParam("longitude") Double longitude,
            @RequestParam("latitude") Double latitude,
            @RequestParam("organizationId") Long organizationId
    ) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Long id = SnowFlow.getId();
        Event event = new Event();
        event.setId(id);
        event.setEventName(event_name);
        event.setOrganizationId(organizationId);
        event.setAddress(address);
        event.setType(type);
        event.setDepartment(department);
        event.setStartTime(LocalDateTime.parse(start_time, df));
        event.setEndTime(LocalDateTime.parse(end_time, df));
        event.setName(name);
        event.setSignIn(sign_in);
        event.setIntroduce(introduce);
        event.setImgId(id);
        event.setLongitude(longitude);
        event.setLatitude(latitude);
        UserEvent userEvent = new UserEvent();
        userEvent.setEventId(id);
        userEvent.setUserId(Long.valueOf(userId));
        userEvent.setRole("president");
        userEventMapper.insert(userEvent);
        try {
            ImageUtil.updateImg(img, id);
        } catch (IOException e) {
            return CommonResult.fail("储存图片失败");
        }
        return CommonResult.succ(eventMapper.insert(event));
    }

    // 实现更新
    @RequestMapping("/update")
    public CommonResult updateMenu(
            @RequestBody Event event
//            @RequestParam("id") Long id,
//            @RequestParam("eventName") String event_name,
//            @RequestParam("address") String address,
//            @RequestParam("type") String type,
//            @RequestParam("department") String department,
//            @RequestParam("startTime") String start_time,
//            @RequestParam("endTime") String end_time,
//            @RequestParam("name") String name,
//            @RequestParam("signIn") Boolean sign_in,
//            @RequestParam("introduce") String introduce,
//            @RequestParam("longitude") Double longitude,
//            @RequestParam("latitude") Double latitude,
//            @RequestParam("organizationId") Long organizationId
    ) {
//        QueryWrapper<Event> eventQueryWrapper = new QueryWrapper<>();
//        eventQueryWrapper.eq("id", id);
//        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        Event event = new Event();
//        event.setEventName(event_name);
//        event.setOrganizationId(organizationId);
//        event.setAddress(address);
//        event.setType(type);
//        event.setDepartment(department);
//        event.setStartTime(LocalDateTime.parse(start_time, df));
//        event.setEndTime(LocalDateTime.parse(end_time, df));
//        event.setName(name);
//        event.setSignIn(sign_in);
//        event.setIntroduce(introduce);
//        event.setLongitude(longitude);
//        event.setLatitude(latitude);
        return CommonResult.succ(eventMapper.updateById(event));
    }

    @GetMapping("/test")
    public String test() {
        return "hello";
    }

}

